package com.wbao.dianniu.logical;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author yejianjun
 *
 */
public class SharedPreferencesUtils {
	
	private static final String MY_SETTTING_PREFERENCES = "my_settting_share_preferences";
	/**	是否为第一次打开app	*/
	private static final String APPFIRSTOPEN_PREFERENCES = "app_first_open_preferences";
	private static SharedPreferences mSettingPreferences;
	private static final String AUTO_LOGIN_PREFERENCES = "auto_login_preferences";
	private static final String USER_NAME_PREFERENCES = "user_name_preferences";
	private static final String PASSWORD_PREFERENCES = "password_preferences";
	private static final String AUTO_UPDATE_TIME = "auto_update_time";
//	private static final String USER_ACCOUTID_PREFERENCES = "user_accountId_preferences";
	
	public static void init(Context context) {
		mSettingPreferences = context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
	}

	public static void relese() {
		mSettingPreferences = null;
	}
	
	public static void clear() {
		SharedPreferences.Editor editor = mSettingPreferences.edit();
		editor.putString(APPFIRSTOPEN_PREFERENCES, null);
		editor.putString(AUTO_LOGIN_PREFERENCES, null);
		editor.commit();
	}

	@SuppressLint("NewApi")
	public static boolean getIsFirstOpen(Context context) {
		if(null == mSettingPreferences){
			mSettingPreferences =  context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
		}
		return mSettingPreferences.getBoolean(APPFIRSTOPEN_PREFERENCES, true);
	}

	@SuppressLint("NewApi")
	public static void setIsFirstOpen(boolean isFirstOpen) {
		SharedPreferences.Editor editor = mSettingPreferences.edit();
		editor.putBoolean(APPFIRSTOPEN_PREFERENCES, isFirstOpen);
		editor.commit();
	}
	
	/**
	 * 设置自动登录状态
	 * 
	 * @param auto
	 */
	public static void setAutoLoginStatus(boolean auto){
		SharedPreferences.Editor editor = mSettingPreferences.edit();
		editor.putBoolean(AUTO_LOGIN_PREFERENCES, auto);
		editor.commit();
	}
	
	
	/**
	 * 获取自动登录状态
	 * 
	 * @return
	 */
	public static boolean getAutoLoginStatus(){
		return mSettingPreferences.getBoolean(AUTO_LOGIN_PREFERENCES, false);
	}
	/**
	 *设置用户ID
	 * 
	 * @param username
	 */
//	public static void setAccountId(String accountId){
//		SharedPreferences.Editor editor = mSettingPreferences.edit();
//		editor.putString(USER_ACCOUTID_PREFERENCES, accountId);
//		editor.commit();
//	}
	
	/**
	 * 获取用户ID
	 * 
	 * @return
	 */
//	public  static String getAccountId(){
//		return mSettingPreferences.getString(USER_ACCOUTID_PREFERENCES, "");
//	}
	
	/**
	 *设置用户名，登录后台时的手机号码
	 * 
	 * @param username
	 */
	public static void setUserName(String username){
		SharedPreferences.Editor editor = mSettingPreferences.edit();
		editor.putString(USER_NAME_PREFERENCES, username);
		editor.commit();
	}
	
	/**
	 * 获取用户名，登录后台时的手机号码
	 * 
	 * @return
	 */
	public  static String getUserName(){
		return mSettingPreferences.getString(USER_NAME_PREFERENCES, "");
	}
	
	/**
	 * 设置密码
	 * 
	 * @param password
	 */
	public static void setPassword(String password){
		SharedPreferences.Editor editor = mSettingPreferences.edit();
		editor.putString(PASSWORD_PREFERENCES, password);
		editor.commit();
	}
	
	/**
	 * 获取密码
	 * 
	 * @return
	 */
	public  static String getPassword(){
		return mSettingPreferences.getString(PASSWORD_PREFERENCES, "");
	}
	
	/**
	 * 设置检测更新时间
	 * 
	 * @param password
	 */
	public static void setUpdateTime(long time){
		SharedPreferences.Editor editor = mSettingPreferences.edit();
		editor.putLong(AUTO_UPDATE_TIME, time);
		editor.commit();
	}
	
	/**
	 * 获取上次检测更新时间
	 * 
	 * @return
	 */
	public  static long getUpdateTime(Context context){
		if(null == mSettingPreferences){
			mSettingPreferences =  context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
		}
		return mSettingPreferences.getLong(AUTO_UPDATE_TIME, 0);
	}
	
//	public static boolean getIsFirstStart(Context context){
//		if(null == mSettingPreferences){
//			mSettingPreferences =  context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
//		}
//		return mSettingPreferences.getBoolean(IS_FIRST_START, true);
//	}
	
//	public static void setIsFirstStart(boolean status){
//		SharedPreferences.Editor editor = mSettingPreferences.edit();
//		editor.putBoolean(IS_FIRST_START, status);
//		editor.commit();
//	}
	
}
